import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "@/store/modules/user";

const routes = [
	{
		path: "/login",
		name: "Login",
		component: () => import("@/views/Login.vue"),
		meta: {
			requiresAuth: false // 不需要登录
		}
	},
	{
		path: "/",
		name: "Index",
		component: () => import("@/views/Index.vue"),
		redirect:"/main",
		children:[
			{
				path:"main",
				name:"Main",
				component: () => import("@/views/Main.vue"),
				meta: {
					requiresAuth: false // 首页不需要登录
				}
			},
			// 用户端路由 - 需要普通用户权限
			{
				path: "demand-hall",
				name: "DemandHall",
				component: () => import("@/views/DemandHall.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [2] // 普通用户和企业用户可以访问
				}
			},
			{
				path: "order-center",
				name: "OrderCenter",
				component: () => import("@/views/OrderCenter.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [1, 2] // 普通用户和企业用户可以访问
				}
			},
			{
				path: "publish-demand",
				name: "PublishDemand",
				component: () => import("@/views/PublishDemand.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [1, 2] // 普通用户和企业用户可以访问
				}
			},
			{
				path: "demand-enterprise-select",
				name: "DemandEnterpriseSelect",
				component: () => import("@/views/DemandEnterpriseSelect.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [1] // 只有普通用户可以访问
				}
			},
			// 普通用户专属路由
			{
				path: "my-demands",
				name: "MyDemands",
				component: () => import("@/views/MyDemands.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [1] // 只有普通用户可以访问
				}
			},
			{
				path: "user-center",
				name: "UserCenter",
				component: () => import("@/views/UserCenter.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [1] // 只有普通用户可以访问
				}
			},
			// 企业用户专属路由
			{
				path: "enterprise-manage",
				name: "EnterpriseManage",
				component: () => import("@/views/EnterpriseManage.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [2] // 只有企业用户可以访问
				}
			},
			{
				path: "enterprise-center",
				name: "EnterpriseCenter",
				component: () => import("@/views/EnterpriseCenter.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [2] // 只有企业用户可以访问
				}
			},
			// 平台管理员专属路由
			{
				path: "demand-management",
				name: "DemandManagement",
				component: () => import("@/views/DemandManagement.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [3] // 只有平台管理员可以访问
				}
			},
			{
				path: "enterprise-management",
				name: "EnterpriseManagement",
				component: () => import("@/views/EnterpriseManagement.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [3] // 只有平台管理员可以访问
				}
			},
			{
				path: "user-management",
				name: "UserManagement",
				component: () => import("@/views/UserManagement.vue"),
				meta: {
					requiresAuth: true,
					userTypes: [3] // 只有平台管理员可以访问
				}
			},
		]
	},
	// 404页面
	{
		path: "/:pathMatch(.*)*",
		redirect: "/"
	}
];

const router = createRouter({
	history: createWebHistory(),
	routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
	const userStore = useUserStore();
	
	// 恢复用户信息（如果有）
	userStore.recoverUserInfo();
	
	// 检查路由是否需要认证
	if (to.meta.requiresAuth) {
		// 需要登录
		if (!userStore.userInfo.isLoggedIn) {
			// 未登录，重定向到登录页
			next({ path: '/login', query: { redirect: to.fullPath } });
		} else {
			// 已登录，检查用户权限
			if (to.meta.userTypes && to.meta.userTypes.length > 0) {
				// 检查用户类型是否匹配
				const hasPermission = userStore.checkPermission(to.meta.userTypes);
				if (hasPermission) {
					// 有权限，放行
					next();
				} else {
					// 无权限，重定向到首页
					console.warn('权限不足，无法访问:', to.fullPath);
					next({ path: '/' });
				}
			} else {
				// 没有指定用户类型限制，所有登录用户都可以访问
				next();
			}
		}
	} else {
		// 不需要登录的页面，直接放行
		next();
	}
});

export default router;
